from fastapi.security.utils import get_authorization_scheme_param
from fastapi import Request

from app.utils.response import UnicornException, NotAuthorizedException


class TokenVerify(object):
    """ Scheme that checks the validity of the authorization token
        that is exchanged prior to authenticating the user in the
        service and issuing the final access token.
    """

    async def __call__(self, request: Request):
        # 验证token
        authorization: str = request.headers.get("Authorization")
        scheme, access_token = get_authorization_scheme_param(authorization)
        if not authorization or scheme.lower() != "bearer":
            raise UnicornException(msg='请登录')
        if access_token != 'alskdfz':
            raise NotAuthorizedException(msg='TOKEN无效')
